#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "stm32f10x.h"

#include "public_lib.h"
#include "serial.h"
#include "system_timer.h"
#include "esp8266.h"
#include "esp8266_task.h"

//typedef struct
//{
//    
//}s_esp8266_task_info;

//static s_esp8266_task_info info = {};

static char temp_data[2048];
int read_len = 0;
void start_esp8266_task(void *args)
{
    esp8266_init(115200, (void *)0, 0);
}

int esp8266_task_run(void *args)
{    
    if((read_len = esp8266_get_data(temp_data, sizeof(temp_data))) > 0)
    {
        for(int i=0;i<read_len;i++)
        {
            serial_putchar(temp_data[i]);
        }
    }
    return 0;
}

int esp8266_task_manage(int argc, char **argv, void *args)
{
    int err_code = -1, success = 0;

    if(argc >= 2)
    {       
        if(strcasecmp(argv[0], "esp8266")==0)
        {
            s_timeval time = {0};
            if(strcasecmp(argv[1], "set_mode") == 0)
            {
                s_wifi_cwmode_para para = {0};
                para.action = 2;
                para.mode = 3;
                esp8266_send_wifi_cmd(WIFI_CWMODE, &para, temp_data, sizeof(temp_data));
                get_timer_time(&time); 
                while(0 == (success = esp8266_get_wifi_cmd_ack(WIFI_CWMODE)))
                {        
                    if(get_diff_time(&time, get_timer_time(NULL)) > 5000)
                    {                
                        break;
                    }
                }
                if(1 == success)
                {
                    serial_writeline("esp8266 set mode[%s] ok", (para.mode==1) ? "STATION" : ((para.mode==2) ? "AP" : "AP+STATION"));
                    err_code = 0;
                }
                else
                {
                    serial_writeline("esp8266 set mode[%s] %s", (para.mode==1) ? "STATION" : ((para.mode==2) ? "AP" : "AP+STATION"), success==0 ? "timeout" : "failed");                    
                }               
            }
            else if(strcasecmp(argv[1], "set_my_ap") == 0)
            {                
                s_wifi_cwsap_para para = {0};
                para.action = 1;
                para.ch = 6;
                para.ecn = 4;
                strcpy(para.pwd, "12345678");
                strcpy(para.ssid, "4f_test");
                esp8266_send_wifi_cmd(WIFI_CWSAP, &para, temp_data, sizeof(temp_data));
                get_timer_time(&time); 
                while(0 == (success = esp8266_get_wifi_cmd_ack(WIFI_CWSAP)))
                {        
                    if(get_diff_time(&time, get_timer_time(NULL)) > 5000)
                    {                
                        break;
                    }
                }
                if(1 == success)
                {
                    serial_writeline("esp8266 set my_ap info ok, info[%s-%s]\r\n", para.ssid, para.pwd);
                    err_code = 0;
                }
                else
                {
                    serial_writeline("esp8266 set my_ap info %s", success==0 ? "timeout" : "failed");
                }
            } 
            else if(strcasecmp(argv[1], "show_my_ap") == 0)            
            {
                s_wifi_cwsap_para para = {0};
                para.action = 0;
                esp8266_send_wifi_cmd(WIFI_CWSAP, &para, temp_data, sizeof(temp_data));
                get_timer_time(&time); 
                while(0 == (success = esp8266_get_wifi_cmd_ack(WIFI_CWSAP)))
                {        
                    if(get_diff_time(&time, get_timer_time(NULL)) > 5000)
                    {                
                        break;
                    }
                }
                if(1 == success)
                {
                    serial_writeline("esp8266 get my_ap info ok, info[%s]\r\n", temp_data);
                    err_code = 0;
                }
                else
                {
                    serial_writeline("esp8266 get my_ap info %s", success==0 ? "timeout" : "failed");
                }
            }
            else if(strcasecmp(argv[1], "show_ap_list") == 0)   
            {
                s_wifi_cwlap_para para = {0};
                para.action = 0;        
                esp8266_send_wifi_cmd(WIFI_CWLAP, &para, temp_data, sizeof(temp_data));
                get_timer_time(&time); 
                while(0 == (success = esp8266_get_wifi_cmd_ack(WIFI_CWLAP)))
                {        
                    if(get_diff_time(&time, get_timer_time(NULL)) > 5000)
                    {                
                        break;
                    }
                }
                if(1 == success)
                {
                    serial_writeline("esp8266 get ap_list ok : %s\r\n", temp_data);
                    err_code = 0;
                }
                else
                {
                    serial_writeline("esp8266 get ap_list %s", success==0 ? "timeout" : "failed");
                }
            }
            else if(strcasecmp(argv[1], "join_ap") == 0)      
            {
                s_wifi_cwjap_para para = {0};
                para.action = 1;
//                strcpy(para.ssid, "NVT_Guest");        
//                strcpy(para.pwd, "nextvision@2020");   
                strcpy(para.ssid, "Honor V10");        
                strcpy(para.pwd, "asdfghjkl");
                esp8266_send_wifi_cmd(WIFI_CWJAP, &para, temp_data, sizeof(temp_data));
                get_timer_time(&time); 
                while(0 == (success = esp8266_get_wifi_cmd_ack(WIFI_CWJAP)))
                {        
                    if(get_diff_time(&time, get_timer_time(NULL)) > 10000)
                    {                
                        break;
                    }
                }
                if(1 == success)
                {
                    serial_writeline("esp8266 join ap ok\r\n");
                    err_code = 0;
                }
                else
                {
                    serial_writeline("esp8266 join ap %s", success==0 ? "timeout" : "failed");
                }
            }
            else if(strcasecmp(argv[1], "show_ap") == 0)  
            {
                s_wifi_cwjap_para para = {0};
                para.action = 0; 
                esp8266_send_wifi_cmd(WIFI_CWJAP, &para, temp_data, sizeof(temp_data));
                get_timer_time(&time); 
                while(0 == (success = esp8266_get_wifi_cmd_ack(WIFI_CWJAP)))
                {        
                    if(get_diff_time(&time, get_timer_time(NULL)) > 5000)
                    {                
                        break;
                    }
                }
                if(1 == success)
                {
                    serial_writeline("esp8266 get ap info %s\r\n", temp_data);
                    err_code = 0;
                }
                else
                {
                    serial_writeline("esp8266 get ap info %s", success==0 ? "timeout" : "failed");
                }
            }
            else if(strcasecmp(argv[1], "quit_ap") == 0)  
            {
                s_wifi_cwqap_para para = {0};
                para.action = 1; 
                esp8266_send_wifi_cmd(WIFI_CWQAP, &para, temp_data, sizeof(temp_data));
                get_timer_time(&time); 
                while(0 == (success = esp8266_get_wifi_cmd_ack(WIFI_CWQAP)))
                {        
                    if(get_diff_time(&time, get_timer_time(NULL)) > 5000)
                    {                
                        break;
                    }
                }
                if(1 == success)
                {
                    serial_writeline("esp8266 quit ap success %s\r\n", temp_data);
                    err_code = 0;
                }
                else
                {
                    serial_writeline("esp8266 quit ap %s",success==0 ? "timeout" : "failed");
                }
            }   
            else if(strcasecmp(argv[1], "set_my_ip") == 0)  
            {
                if(argc == 4)
                {
                    s_wifi_cipapip_para ap_para = {0};
                    s_wifi_cipstaip_para station_para = {0};
                    WIFI_AT cmd = WIFI_CIPAPIP;
                    if(atoi(argv[2]) == 0)
                    {
                        ap_para.action = 1;
                        strcpy(ap_para.ip, argv[3]);
                        esp8266_send_wifi_cmd(cmd, &ap_para, temp_data, sizeof(temp_data));
                    }
                    {
                        station_para.action = 1;
                        strcpy(ap_para.ip, argv[3]);
                        cmd = WIFI_CIPSTAIP;
                        esp8266_send_wifi_cmd(cmd, &station_para, temp_data, sizeof(temp_data));
                    }                    
                    
                    get_timer_time(&time); 
                    while(0 == (success = esp8266_get_wifi_cmd_ack(cmd)))
                    {        
                        if(get_diff_time(&time, get_timer_time(NULL)) > 5000)
                        {                
                            break;
                        }
                    }
                    
                    if(1 == success)
                    {
                        serial_writeline("esp8266 set my_ip[%s] success %s\r\n", (atoi(argv[2]) == 0) ? "AP" : "STATION", temp_data);
                        err_code = 0;
                    }
                    else
                    {
                        serial_writeline("esp8266 set my_ip[%s] %s", (atoi(argv[2]) == 0) ? "AP" : "STATION", success==0 ? "timeout" : "failed");
                    }
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s 0/1[0-ap 1-station] ip[192.168.xxx.xxx]", argv[0], argv[1], argv[0], argv[1]);
                }
            }
            else if(strcasecmp(argv[1], "show_my_ip") == 0)  
            {
                if(argc == 3)
                {
                    s_wifi_cipapip_para ap_para = {0};
                    s_wifi_cipstaip_para station_para = {0};
                    WIFI_AT cmd = WIFI_CIPAPIP;
                    if(atoi(argv[2]) == 0)
                    {
                        ap_para.action = 0;
                        esp8266_send_wifi_cmd(cmd, &ap_para, temp_data, sizeof(temp_data));
                    }
                    {
                        station_para.action = 0;
                        cmd = WIFI_CIPSTAIP;
                        esp8266_send_wifi_cmd(cmd, &station_para, temp_data, sizeof(temp_data));
                    }                    
                    
                    get_timer_time(&time); 
                    while(0 == (success = esp8266_get_wifi_cmd_ack(cmd)))
                    {        
                        if(get_diff_time(&time, get_timer_time(NULL)) > 5000)
                        {                
                            break;
                        }
                    }
                    
                    if(1 == success)
                    {
                        serial_writeline("esp8266 show my_ip[%s] success %s\r\n", (atoi(argv[2]) == 0) ? "AP" : "STATION", temp_data);
                        err_code = 0;
                    }
                    else
                    {
                        serial_writeline("esp8266 show my_ip[%s] %s", (atoi(argv[2]) == 0) ? "AP" : "STATION", success==0 ? "timeout" : "failed");
                    }
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s 0/1[0-ap 1-station]", argv[0], argv[1], argv[0], argv[1]);
                }
            }
            else if(strcasecmp(argv[1], "set_my_mac") == 0)  
            {
                if(argc == 4)
                {
                    s_wifi_cipapmac_para ap_para = {0};
                    s_wifi_cipstamac_para station_para = {0};
                    WIFI_AT cmd = WIFI_CIPAPMAC;
                    if(atoi(argv[2]) == 0)
                    {
                        ap_para.action = 1;
                        strcpy(ap_para.mac, argv[3]);
                        esp8266_send_wifi_cmd(cmd, &ap_para, temp_data, sizeof(temp_data));
                    }
                    {
                        station_para.action = 1;
                        strcpy(station_para.mac, argv[3]);
                        cmd = WIFI_CIPSTAMAC;
                        esp8266_send_wifi_cmd(cmd, &station_para, temp_data, sizeof(temp_data));
                    }                    
                    
                    get_timer_time(&time); 
                    while(0 == (success = esp8266_get_wifi_cmd_ack(cmd)))
                    {        
                        if(get_diff_time(&time, get_timer_time(NULL)) > 5000)
                        {                
                            break;
                        }
                    }
                    
                    if(1 == success)
                    {
                        serial_writeline("esp8266 set my_mac[%s] success %s\r\n", (atoi(argv[2]) == 0) ? "AP" : "STATION", temp_data);
                        err_code = 0;
                    }
                    else 
                    {
                        serial_writeline("esp8266 set my_mac[%s] %s", (atoi(argv[2]) == 0) ? "AP" : "STATION", success==0 ? "timeout" : "failed");
                    }
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s 0/1[0-ap 1-station] ip[192.168.xxx.xxx]", argv[0], argv[1], argv[0], argv[1]);
                }
            }
            else if(strcasecmp(argv[1], "show_my_mac") == 0)  
            {
                if(argc == 3)
                {
                    s_wifi_cipapmac_para ap_para = {0};
                    s_wifi_cipstamac_para station_para = {0};
                    WIFI_AT cmd = WIFI_CIPAPMAC;
                    if(atoi(argv[2]) == 0)
                    {
                        ap_para.action = 0;
                        esp8266_send_wifi_cmd(cmd, &ap_para, temp_data, sizeof(temp_data));
                    }
                    {
                        station_para.action = 0;
                        cmd = WIFI_CIPSTAMAC;
                        esp8266_send_wifi_cmd(cmd, &station_para, temp_data, sizeof(temp_data));
                    }                    
                    
                    get_timer_time(&time); 
                    while(0 == (success = esp8266_get_wifi_cmd_ack(cmd)))
                    {        
                        if(get_diff_time(&time, get_timer_time(NULL)) > 5000)
                        {                
                            break;
                        }
                    }
                    
                    if(1 == success)
                    {
                        serial_writeline("esp8266 show my_mac[%s] success %s\r\n", (atoi(argv[2]) == 0) ? "AP" : "STATION", temp_data);
                        err_code = 0;
                    }
                    else
                    {
                        serial_writeline("esp8266 show my_mac[%s] %s", (atoi(argv[2]) == 0) ? "AP" : "STATION", success==0 ? "timeout" : "failed");
                    }
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s 0/1[0-ap 1-station]", argv[0], argv[1], argv[0], argv[1]);
                }
            }
            else if(strcasecmp(argv[1], "start_my_client") == 0)
            {
                if(argc == 4)
                {
                    s_endpoint_config cfg = {0};
                    cfg.mode = 0;
                    cfg.mux = 1;
                    cfg.recv_queue = pq_create_queue();
                    err_code = esp8266_config_endpoint(cfg, argv[2], atoi(argv[3]));
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s ip[A.B.C.D] port", argv[0], argv[1], argv[0], argv[1]);
                }
            }
            else if(strcasecmp(argv[1], "my_client_send") == 0)
            {
                char *ptr = "helloworld";                
                if(argc == 3)
                {
                    err_code = esp8266_send_data(atoi(argv[2]), ptr, strlen(ptr));
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s id[0-5]", argv[0], argv[1], argv[0], argv[1]);
                }                
            }
            else if(strcasecmp(argv[1], "my_client_close") == 0)
            {                
                if(argc == 3)
                {
                    err_code = esp8266_close_socket(atoi(argv[2]));
                }
                else
                {
                    serial_writeline("%s %s usage:  %s %s id[0-5]", argv[0], argv[1], argv[0], argv[1]);
                }
            }
            else if(strcasecmp(argv[1], "start_my_server") == 0)
            {
                s_endpoint_config cfg = {0};
                cfg.mode = 1;
                cfg.recv_queue = pq_create_queue();
                err_code = esp8266_config_endpoint(cfg, "", 8080);
            }            
        }        
        else{/* not for me, do nothing */}
    }
    else{/* not for me, do nothing */}

    return err_code;
}
